package com.blogs.service;


import java.util.Map;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import com.blogs.entity.AppConfig;
import com.blogs.entity.MsgEntity;
import com.blogs.entity.VerSion;
import com.blogs.service.GetMsgTask.onTaskCallBack;
import com.blogs.tools.Tools;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class CheckVersionTask {
	public interface IMsgCallBack {
		public void onSuccess(VerSion vs);
		public void onFailed();
	}
	private String url=AppConfig.GET_CHECK_VERSION_RESULT;
	private GetMsgTask getresult;
	private IMsgCallBack callback;
	public CheckVersionTask(Context context, IMsgCallBack callback)
	{
		this.callback=callback;
		PackageInfo pinfo=Tools.GetPackageInfo(context);
		url=url.replace("{0}", pinfo.versionCode+"");
		getresult=new GetMsgTask(taskCallBack);
	}
	public void GetResult()
	{
		getresult.execute(url);
	}
	private GetMsgTask.onTaskCallBack taskCallBack=new onTaskCallBack() {
		public void onSuccess(String result) {
			GsonBuilder builder = new GsonBuilder();
			Gson gson = builder.create();
			MsgEntity<Map<String, Object>> msg=null;
			try
			{
				msg=gson.fromJson(result,MsgEntity.class);
			}
			catch(Exception e)
			{
				callback.onFailed();
				return;
			}
			if(msg.op.equals(""))
			{
				callback.onFailed();
				return;
			}
			VerSion vs=new VerSion(msg.data);
			callback.onSuccess(vs);
		};
		public void onFailed() {
			callback.onFailed();
		};
	};
}
